﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PdfSharp;
using WidgetsDesign.API.Dtos;
using WidgetsDesign.API.Models;
using WidgetsDesign.API.Services;

namespace WidgetsDesign.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RiskRepositoryController : ControllerBase
    {
        private readonly IRiskRepositoryRepository _riskRepositoryRepository;
        private readonly IMapper _Mapper;
        public RiskRepositoryController(IRiskRepositoryRepository riskRepositoryRepository, IMapper mapper)
        {
            _riskRepositoryRepository = riskRepositoryRepository;
            _Mapper = mapper;
        }
        /// <summary>
        /// 获取风险库列表
        /// </summary>
        /// <param name="type"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>

        [HttpGet]
        public async Task<IActionResult> GetRiskRepositoryList([FromQuery] string type, int pageIndex, int pageSize)
        {
            var result = await _riskRepositoryRepository.GetRiskRepositoryListAsync(type, pageIndex, pageSize);
            var total =  _riskRepositoryRepository.GetRiskRepositoryListTotal();
            return Ok(new { ret = true, code = 200, data = result, total });
        }
        /// <summary>
        /// 添加风险
        /// </summary>
        /// <param name="riskRepositoryDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddRiskRepository([FromBody] RiskRepositoryDto riskRepositoryDto)
        {
            riskRepositoryDto.Id = Guid.NewGuid().ToString();
            riskRepositoryDto.Time = DateTime.Now;
            riskRepositoryDto.Create_time = DateTime.Now;
            var RiskRepositoryModel = _Mapper.Map<RiskRepositoryModel>(riskRepositoryDto);
            _riskRepositoryRepository.AddRiskRepository(RiskRepositoryModel);
            await _riskRepositoryRepository.SaveAsync();
            return Ok(new { ret = true, code = 200, data = new { } });
        }
        /// <summary>
        /// 获取风险详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetRiskRepositoryDetail([FromQuery] string id)
        {
            var result = await _riskRepositoryRepository.GetRiskRepositoryAsync(id);
            return Ok(new { ret = true, code = 200, data = result });
        }
        [HttpPost]
        public IActionResult UpdateRiskRepository([FromBody] RiskRepositoryDto riskRepositoryDto)
        {
            riskRepositoryDto.Update_time = DateTime.Now;
            var RiskRepositoryModel = _Mapper.Map<RiskRepositoryModel>(riskRepositoryDto);
             _riskRepositoryRepository.UpdateRiskRepository(RiskRepositoryModel);
            return Ok(new { ret = true, code = 200, data = new { } });
        }
        /*  
        
          [HttpPost]
          public async Task<IActionResult> CreateHitokoto([FromBody] RiskRepositoryDto hitokotoDto)
          {
              var HitokotoModel = _Mapper.Map<HitokotoModel>(hitokotoDto);

              if(await _hitokotoRepository.GetHitokotoExistsAsync(hitokotoDto.Id))
              {
                  return BadRequest();
              }
              _hitokotoRepository.CreateHitokoto(HitokotoModel);
              await _hitokotoRepository.SaveAsync();
              return Ok();
          }
        */
    }

}


